<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>11 深度侦听</title>
</head>
<body>
  <div id="app">
    <input type="text" v-model="user.firstName"> +
    <input type="text" v-model="user.lastName"> = {{ user.fullName }}

  </div>
</body>
<script src="lib/vue.js"></script>
<script>
  new Vue({
    el: '#app',
    data: {
      user: {
        firstName: '',
        lastName: '',
        fullName: ''
      }
    },
    watch: {
      // user (newVal, oldVal) { // 无法监听
      //   console.log(newVal, oldVal)
      // }
      
      // 'user.firstName' (newVal, oldVal) {
      //   console.log(newVal, oldVal)
      //   this.user.fullName = newVal + this.user.lastName
      // },
      // 'user.lastName' (newVal, oldVal) {
      //   console.log(newVal, oldVal)
      //   this.user.fullName = this.user.firstName + newVal
      // }

      // user: {
      //   deep: true, // 深度侦听
      //   handler (newVal, oldVal) {
      //     console.log(newVal, oldVal)
      //     this.user.fullName = newVal.firstName + newVal.lastName
      //   }
      // }

      user: {
        deep: true, // 深度侦听
        immediate: true, // 第一次就监听属性
        handler (newVal, oldVal) {
          console.log(newVal, oldVal)
          this.user.fullName = newVal.firstName + newVal.lastName
        }
      }
    }
  })
</script>
</html>
